const { regClass } = Laya;
import { StaticData } from "../../Data/StaticData";
import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { EViewType } from "../Data/DataType";
import { EventNames } from "../Data/EventNames";
import { ViewExChangeRunBase } from "./ViewExChangeRun.generated";

@regClass()
export class ViewExChangeRun extends ViewExChangeRunBase {
    private Score: number = 0;
    private Id: number = 0;
    onEnable(): void {
        this.addEvent();
    }
    init(data: { need: string, goodsName: string, img: Laya.Texture, id: number, num: number }) {
        let need = data.need;
        let goodsName = data.goodsName;
        this.Score = parseInt(need);
        this.NeedScore.text = "积分：" + need;
        this.Text.text = goodsName + "x1"
        this.Id = data.id;
        Laya.loader.load(data.img, Laya.Handler.create(this, (texture: Laya.Texture) => {
            if (data.img == data.img) {
                let boxW = 268
                let boxH = 211
                let rate = boxW / boxH;
                let rate2 = texture.width / texture.height;
                if (rate > rate2) {
                    this.icon.height = boxH
                    this.icon.width = boxH * rate2;
                }
                else {
                    this.icon.height = boxW / rate2
                    this.icon.width = boxW;
                }
                this.icon.skin = undefined
                this.icon.source = texture;
            }
        }), undefined, Laya.Loader.IMAGE)
    }


    onDestroy(): void {
        this.removeEvent();
    }
    addEvent() {
        this.btnCancel.on(Laya.Event.CLICK, this, this.onBtnCancel)
        this.btnConfirm.on(Laya.Event.CLICK, this, this.onBtnConfirm)
        Laya.stage.on(EventNames.INIT_SHOP_VIEW, this, this.init)
    }

    removeEvent() {
        this.btnCancel.off(Laya.Event.CLICK, this, this.onBtnCancel)
        this.btnConfirm.off(Laya.Event.CLICK, this, this.onBtnConfirm)
        Laya.stage.off(EventNames.INIT_SHOP_VIEW, this, this.init)
    }

    onBtnCancel() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Tips)
        Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, "已取消兑换")
        this.destroy();
    }
    onBtnConfirm() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Tips)
        HttpManager.post(HttpAPI.POST_SHOPPING_EXCHANGESHOPPING, {
            shoppingId: this.Id
        }, undefined).then(this.sendMsg.bind(this)).catch(this.sendMsg.bind(this));
        
        this.destroy();
    }
    private sendMsg(data: any) {
        if (data.success) {
            StaticData.integral-=this.Score
            Laya.stage.event(EventNames.INTEGRAL_CHANGE, StaticData.integral);
            Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS,"兑换成功")
        }
        else
        {
            Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, data.message)
        }
    }
}